from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Publisher,Book
from .Xlh import PublisherSerializer,BookSerializer
# Create your views here.


class Bookadd(APIView):
    def post(self,request):
        name=request.data.get('name')
        publisher_id=request.data.get('publisher_id')
        book_status=request.data.get('book_status')
        reader=request.data.get('reader')

        if not all([name,publisher_id,book_status]):
            return Response({
                'message':'不能为空'
            })

        if Publisher.objects.filter(id=publisher_id).exists():
            book01=Book.objects.create(name=name,publisher_id=publisher_id,book_status=book_status)
            book01.save()
            serializer=PublisherSerializer(book01)
            return Response({
                'message':'添加成功',
                'data':serializer.data,
            })
